var choosedCallBackFun;//成功选择之后的回调函数
var element_type;

var templateParams = {
    tableTheads:["元素名称", "定位类型", "元素类型", "定位值", "frame", "备注", "操作"],
    btnTools:[{
        select:true,
        id:"list-by-system-node",
        option:[{value: '', text: '全部系统'}]
    },{
        select:true,
        id:"list-by-page-node",
        option:[{value: '', text: '全部页面'}]
    }, {
        type:"danger",
        size:"M",
        id:"query-current-obj",
        name:"查询已选元素"
    }],
    formControls:[

    ]
};

var columnsSetting = [
    {"data":"id", "width": "55"},
    ellipsisData("element_name"),
    {
        "data": "location_by_id",
        "className":"ellipsis",
        "width": "55",
        "render": function(data){
            return queryObjValueInArray(DICT_CONFIG.location_by, data, 'id', 'cn_name');
        }
    },
    {
        "data":"type",
        "className":"ellipsis",
        "width": "55",
    },
    ellipsisData("by_value"),
    ellipsisData("frame_name"),
    longTextData("mark", "element_name", "备注信息"),
    {
        "data":null,
        "width": "30",
        "render":function(data, type, full, meta){
            var context = [{
                title:"选择",
                markClass:"object-choose",
                iconFont:"&#xe6ab;"
            }];
            return btnIconTemplate(context);
        }
    }];

var eventList = {
    '.object-choose':function(){
        choosedCallBackFun(table.row( $(this).parents('tr') ).data());
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    },
    //改变系统
    '#list-by-system-node': {
        'change':function(){
            //使用新参数查询
            let newUrl = pUrl;
            let systemId = $(this).val();
            stepChooseElementPageConfig.systemId = systemId;
            stepChooseElementPageConfig.pageId = '';
            if (systemId != '') {
                newUrl += '&system_id=' + systemId;
                //获取系统下面的页面列表
                GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_ELEMENT.listNode, function(data){
                    let options = '<option value="">全部页面</option>';
                    $.each(data.data, function(i, n) {
                        options = options + '<option value="' + n.id + '">' + n.node_name + '</option>';
                    });

                    $('#list-by-page-node').html(options);
                }, null, true, {parent_node_id: systemId});
            } else {
                $('#list-by-page-node').html('<option value="">全部页面</option>');
            }
            refreshTable(newUrl);
            localStorage.setItem('stepChooseElementPageConfig', JSON.stringify(stepChooseElementPageConfig));
        }
    },
    //改变页面
    '#list-by-page-node': {
        'change': function(){
            //使用新参数查询
            let newUrl =  pUrl;
            let pageId = $(this).val();
            stepChooseElementPageConfig.pageId = pageId;
            if (pageId != '') {
                newUrl += '&node_id=' + pageId;
            } else {
                newUrl += '&system_id=' + $('#list-by-system-node').val();
            }

            refreshTable(newUrl);
            localStorage.setItem('stepChooseElementPageConfig', JSON.stringify(stepChooseElementPageConfig));
        }
    },
    '#query-current-obj':function() {
        if (GLOBAL_UTILS.stringUtils.isBlank(currentElementId)) {
            layer.msg('无已选择的元素!', {time: 1400});
            return;
        }
        table.column( 0 ).search( '^' + currentElementId +'$', true, false).draw();
        $(this).attr('id', 'query-all-obj');
        $(this).text('查询全部元素');
        $(this).removeClass('btn-danger').addClass('btn-primary');
    },
    '#query-all-obj': function(){
        table.column(0).search("").draw();
        $(this).attr('id', 'query-current-obj');
        $(this).text('查询已选元素');
        $(this).removeClass('btn-primary').addClass('btn-danger');
    },
};
var pUrl;
var stepChooseElementPageConfig = {systemId: '', pageId: ''};
var currentElementId;
var mySetting = {
    eventList:eventList,
    templateCallBack:function(df){
        choosedCallBackFun = parent[GetQueryString("callback")];
        element_type = GetQueryString("type") || '';
        currentElementId = GetQueryString("elementId");

        if (GLOBAL_UTILS.stringUtils.isNotBlank(element_type)) {
            $('#query-current-obj').hide();
        }

        //获取上一次的选择
        publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_ELEMENT.listElement + "?type=" + element_type;
        pUrl = REQUEST_URL.WEB_ELEMENT.listElement + "?type=" + element_type;

        if (GLOBAL_UTILS.stringUtils.isBlank(currentElementId)) {
            let a  = localStorage.getItem('stepChooseElementPageConfig');
            if (GLOBAL_UTILS.stringUtils.isNotBlank(a)) {
                stepChooseElementPageConfig = JSON.parse(a);
                if (GLOBAL_UTILS.stringUtils.isNotBlank(stepChooseElementPageConfig.systemId)) {
                    publish.renderParams.listPage.listUrl = pUrl + '&system_id=' + stepChooseElementPageConfig.systemId;
                }

                if (GLOBAL_UTILS.stringUtils.isNotBlank(stepChooseElementPageConfig.pageId)) {
                    publish.renderParams.listPage.listUrl = pUrl + '&node_id=' + stepChooseElementPageConfig.pageId;
                }
            }
        }


        //查询系统节点
        GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_ELEMENT.listNode
            , function(data){
                let options = '<option value="">全部系统</option>';
                $.each(data.data, function(i, n) {
                    options = options + '<option value="' + n.id + '"';
                    if (n.id == stepChooseElementPageConfig.systemId) {
                        options += ' selected '
                    }
                    options += '>' + n.node_name + '</option>';

                });

                $('#list-by-system-node').html(options);

                if (GLOBAL_UTILS.stringUtils.isNotBlank(stepChooseElementPageConfig.systemId)) {
                    GLOBAL_UTILS.ajaxUtils.ajaxGet(REQUEST_URL.WEB_ELEMENT.listNode, function(data){
                        let options = '<option value="">全部页面</option>';
                        $.each(data.data, function(i, n) {
                            options = options + '<option value="' + n.id + '"';
                            if (n.id == stepChooseElementPageConfig.pageId) {
                                options += ' selected ';
                            }

                            options += '>' + n.node_name + '</option>';
                        });

                        $('#list-by-page-node').html(options);
                    }, null, true, {parent_node_id: stepChooseElementPageConfig.systemId});
                }

            }, null, true, {node_level: GLOBAL_PARAM.ELEMENT_NODE_LEVEL.SYSTEM.value});


        df.resolve();
    },
    customCallBack: function(){
        table.column(0).search("").draw();
    },
    listPage:{
        listUrl:REQUEST_URL.WEB_ELEMENT.listElement,
        tableObj:".table-sort",
        columnsSetting:columnsSetting,
        columnsJson:[7],
        dtOtherSetting:{serverSide:false}
    },
    templateParams:templateParams
};

$(function(){
    publish.renderParams = $.extend(true,publish.renderParams,mySetting);
    publish.init();
});
